package controller

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"synthesis-server/dbConn"
	"synthesis-server/middle"
	"synthesis-server/model"
	"synthesis-server/server"
)

func IsExist(c *gin.Context) { //验证用户名和密码并发放token
	Db := dbConn.ConnMysql()
	println("能否从前端拿到form：", c.PostForm("ID"), c.PostForm("pwd"))
	u := &model.User{}
	first := Db.Where("id=? and pwd=?", c.PostForm("ID"), c.PostForm("pwd")).Find(&u)
	fmt.Println("从数据库查到用户:", u)
	if first.Error != nil {
		Db.Close()
		c.JSON(200, gin.H{
			"status": 0,
		})
		return
	}
	println("准备进入生成token")
	Db.Close()
	middle.Setting(c, u)
}
func Add(c *gin.Context) { //添加用户
	Db := dbConn.ConnMysql()
	Db.AutoMigrate(&model.User{})
	user := model.User{c.PostForm("ID"), c.PostForm("pwd"), c.PostForm("type")}
	create := Db.Create(&user)
	Db.Close()
	if create.Error != nil {
		c.JSON(200, gin.H{
			"status": 0,
		})
		return
	}
	c.JSON(200, gin.H{
		"status": 1,
	})
}

/*
返回当前排队人数和在线客服数量
*/
func Status(c *gin.Context) {
	c.JSON(200, gin.H{
		"waitingClients": len(server.MyServer.WaitingClient.Data),
		"onlineServer":   server.MyServer.OnlineServer,
	})

}
